package com.itheima.dao.impl;

import com.itheima.dao.IBookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;


public class BookDaoImpl implements IBookDao {

    JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 减余额
     */
    public void updateBalance(String userName,int price){
        String sql = "update account set balance = balance - ? where username = ?";
        jdbcTemplate.update(sql,price,userName);

    }
    /**
     * 获取某本图书的价格
     */

    public int getPrice(String isbn){
        String sql = "select price from book where isbn=?";
        return jdbcTemplate.queryForObject(sql, Integer.class, isbn);
    }

    /**
     * 减库存
     */
    public void updateStock(String isbn){
        String sql = "update book_stock set stock = stock -1 where isbn=?";
        jdbcTemplate.update(sql,isbn);
    }
}
